import time

from pymongo import MongoClient
from user_pwd import *
import re
import spacy

nlp=spacy.load('en_core_web_sm')
metaFile=open('test.meta', 'w', encoding='utf-8')

def writevrt(text,id,f):
	print(f'<text id="{id}">', file=f)
	s=text
	processedText=re.sub(r"\s+"," ", s.strip())
	processedText=re.sub(r"<","(",processedText)
	processedText=re.sub(r">",")",processedText)
	processedText=re.sub('\t','',processedText)
	doc = nlp(processedText)
	for sentence in doc.sents:
		print('<s>', file=f)
		for token in sentence:
			word=token.text
			lemma=token.lemma_
			if lemma=="-PRON-":
				lemma=token.lower_
			print("\t".join([word, token.tag_, lemma, token.pos_]), file=f)
		print('</s>', file=f)
	print('</text>', file=f)
	#
def writeMetaData(video,text,metaFile):
	print(video['video_id'], video['video_name'],int(video['difficulty']),video.get('is_green',0),video.get('visual_vid',0),video.get('video_img',""),video.get('check_time'),text,sep='\t',file=metaFile)
def writemetaOther(*ags,metaFile):
	print(ags[0],ags[1],file=metaFile,sep='\t')


if __name__ == '__main__':
	f=open('test.vrt', 'w', encoding='utf-8')
	metafile=open('test.meta','w',encoding='utf-8')
	i=0
	File=open('eng_sentences.tsv','r',encoding='utf-8')

	t=time.time()
	for data in File:
		print(i)
		data=data.split('\t')
		#writevrt(data[2],data[0],f)
		writemetaOther(data[0],data[2],metaFile=metaFile)
	print(time.time()-t)
